package com.ycsj.goldmedalnewconcept.common.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.common.util.LpUtils;
import com.ycsj.goldmedalnewconcept.teacher.fragment.ClozeTestFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.FillingSentenceFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.ReadEssayQuestionFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.ReadGapFillingFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.ReadJudgeFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.ReadSingleChooseFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.SingleChoiceFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.TkSentenceCompleFragment;
import com.ycsj.goldmedalnewconcept.teacher.fragment.TranslateFragment;
import com.ycsj.goldmedalnewconcept.teacher.manager.TitleManager;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 单题的题目详情界面
 */
public class SingleQuestionDetailsActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_edit)
    ImageView lvEdit;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;

    //传递过来的页面对象
    private TkItemBean itemBean;

    //学生做题0  1学生查看解析 2老师查看题和解析-有人做(全校正确率和小题正确率都显示) 3(习题分析)老师查看题和解析-没人做,对应查看单题
    private String type;

    //题目类型
    private String questionType;

    //填充的Framgent
    private Fragment fragment;

    /**
     * 跳转到当前页面的方法
     *
     * @param itemBean 单题的Fragment展示的数据对象
     * @param type     用于区分跳转的状态
     */
    public static void goActivity(Context context, TkItemBean itemBean, String type) {
        Intent intent = new Intent(context, SingleQuestionDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("itembean", itemBean);
        bundle.putString("type", type);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_single_question_details;
    }

    @Override
    protected void init() {

        //状态栏沉浸
        initStateBar();

        //初始化标题栏
        initTitle();

        //接受题目对象
        initIntent();

        //初始化标题
        initTitle();

        //根绝不同题目类型填充不同的Fragment
        initContain();

    }

    /**
     * 获取Intent对象
     */
    private void initIntent() {
        Bundle bundle = getIntent().getExtras();
        type = bundle.getString("type");
        itemBean = (TkItemBean) bundle.getSerializable("itembean");
        //获取题目类型
        questionType = itemBean.getTypeid();
    }

    /**
     * 初始化标题栏
     */
    private void initTitle() {
        ivBack.setVisibility(View.INVISIBLE);
        RelativeLayout.LayoutParams lp = LpUtils.getRelatLp(UiUtils.dp2px(36), UiUtils.dp2px(36));
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        lvEdit.setLayoutParams(lp);
        lvEdit.setImageResource(R.drawable.close_circle);
        lvEdit.setVisibility(View.VISIBLE);
        //设置标题
        TitleManager.setTitle(questionType, tvTitle);
    }

    /**
     * 根绝不同类型来填充化不同题型的Fragment
     */
    private void initContain() {
        //根据不同的题目类型初始化不同的Fragment
        switch (questionType) {
            case "1":
                //阅读理解判断(已开放)
                fragment = ReadJudgeFragment.newInstance(itemBean, type,0);
                break;
            case "2":
                //阅读理解填空(已开放)
                fragment = ReadGapFillingFragment.newInstance(itemBean, type,0);
                break;
            case "3":
                //阅读理解选择(已开放)
                fragment = ReadSingleChooseFragment.newInstance(itemBean, type,0);
                break;
            case "4":
                //阅读理解问答(已开放)
                fragment = ReadEssayQuestionFragment.newInstance(itemBean, type,0);
                break;
            case "5":
                //完型填空选择(已开放)
                fragment = ClozeTestFragment.newInstance(itemBean, type,0);
                break;
            case "6":
                //填空题(已开放)
                fragment = FillingSentenceFragment.newInstance(itemBean, type,0);
                break;
            case "7":
                //根据所给词适当形式填空(已开放)
                fragment = TkSentenceCompleFragment.newInstance(itemBean, type,0);
                break;
            case "8":
                //句子改错(未开放)
                break;
            case "9":
                //翻译(已开放)
                fragment = TranslateFragment.newInstance(itemBean, type,0);
                break;
            case "10":
                //选择 单选题(已开放)
                fragment = SingleChoiceFragment.newInstance(itemBean, type + "",0);
                break;
            case "11":
                //连词成句(未开放)
                break;
            case "12":
                //句型转换(未开放)
                break;
            case "13":
                //选词填空 填句子(未开放)
                break;
            case "14":
                //选词填文章 填文章(未开放)
                break;
        }

        //将初始化的Fragment添加到容器
        fillFragment(fragment);
    }


    /**
     * 填充Fragment到FrameLayout的方法
     */
    private void fillFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().add(R.id.fl_contain, fragment).commit();
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @OnClick({R.id.lv_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lv_edit:
                //关闭
                finish();
                break;
        }
    }
}
